<?php

function upload_files($tmps, $destination) {
	$ret = true;
	
	foreach ($tmps as $tmp) {
		$ret = $ret && upload_file($tmp, $destination);
	}
	
	return $ret;
}

function upload_file($tmp, $destination) {
	
	if (! is_dir($destination)) {
		mkdir($destination, 0777, true);
	}
	
	return move_uploaded_file($tmp['tmp_name'], $destination . DS . $tmp['name']);
}

function remove_other_files ($files, $path) {
	$ret = 0;
	if ($dir = opendir($path)) {
		while($f = readdir($dir)) {
			if (!preg_match('/^\.+$/', $f) && !in_array($f, $files)) {
				$ret++;
				unlink($path . "/" . $f);
			}
		}
	}
	return $ret;
}

function remove_dir($path) {
	if ($dir = opendir($path)) {
		while($f = readdir($dir)) {
			if (!preg_match('/^\.+$/', $f)) {
				@unlink($path . "/" . $f);
			}
		}
		rmdir($path);
	}
}

function date_local($date, $long=false) {
    $monthNames = Configure::read('month-names');
    
    $time = strtotime($date);
    if (! $time) {
        return '???';
    }
    
    $month = $monthNames[(int)date("m", $time)-1];
    if (! $long) {
        $month = substr($month, 0, 3);
    }
    
    return date("d", $time) . "." . $month . " " . date("Y", $time);
}

function currency_format($number, $decimals=2, $sepDecimal=",", $sepThousand=".") {
    return number_format($number, $decimals, $sepDecimal, $sepThousand);
}

function calculateNumMonths($start, $end) {
	$start = strtotime($start);
    $end   = strtotime($end);
	
	$time = $start;
    $startDay   = (int)date('d', $start);
    $startMonth = (int)date('m', $start);
    $prevMonth  = $startMonth;
    $numMonths = 1;
    while($time < $end) {
        $time += 60*60*24;
        
        $m = (int)date('m', $time);
        $d = (int)date('d', $time);
        
        if ($m != $prevMonth && (($m-$startMonth>2) || $d>$startDay)) {
            $prevMonth = $m;
            $numMonths++;
        }
        //echo "$startDay.$prevMonth ? $d.$m => $numMonths<br> ";
    }

	return $numMonths;
}

function month_last_day($date) {
    $time = totime($date);
    
    $d = date('d', $time);
    $m = date('m', $time);
    $y = date('Y', $time);
    
    // 31
    if (in_array($m, array(1,3,5,7,8,10,12))) {
        $lastDay = 31;
        
    // 30
    } else if (in_array($m, array(4,6,9,11))) {
        $lastDay = 30;
        
    // febrauary
    } else {
        
        // 28
        if ($y%4) {
            $lastDay = 28;
            
        // 29
        } else {
            $lastDay = 29;
        }
    }
    
    return date('Y-m-d', totime("{$y}-{$m}-{$lastDay}"));
}

function totime($date) {
    preg_match_all('/^(\d+)-(\d+)-(\d+)( \d+:\d+:\d+)?$/', $date, $matches);
    $year = $month = $day = $hour = $minute = $second = 0;
    
    if (! empty($matches[0])) {
        $year  = $matches[1][0];
        $month = $matches[2][0];
        $day   = $matches[3][0];
        
        if (! empty($matches[4][0])) {
            list($hour, $minute, $second) = explode(":", trim($matches[4][0]));
        }
    }
    
    return gmmktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year);
}
?>